package me.leetcodepractice;

public class E10Leetcode141 {
    // 检测链表是否有环
    public boolean hasCycle(ListNode head) {
        ListNode h = head; // 兔子
        ListNode t = head; // 龟
        while (h != null && h.next != null) {
            t = t.next;
            h = h.next.next;
            if (h == t) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        ListNode n5 = new ListNode(5, null);
        ListNode n4 = new ListNode(4, n5);
        ListNode n3 = new ListNode(3, n4);
        ListNode n2 = new ListNode(2, n3);
        ListNode n1 = new ListNode(1, n2);

        //n5.next = n3;
        System.out.println(new E10Leetcode141().hasCycle(n1));
    }
}
